clear all

**Save to update later
global BS_fold_old = "$BS_fold"
global working_old = "$working"
global tempfold_old = "$tempfold"
global clean_old = "$clean"

global spec = "new_klme"
local spec = "$spec"

global tempfold = "$BS_fold/local6/" // **UPDATING TEMPFOLD TO BE A LOCAL FOLDER FOR EACH BOOTSTRAP;
global workingnew = "$BS_fold/local6/inputs/"
global BSfoldnew = "$BS_fold/local6/final/"
global cleannew = "$BS_fold/local6/inputs/"

use "$working/prowess_wits_estimation_qonly_klme.dta", clear
save "$workingnew/prowess_wits_estimation_qonly_klme.dta", replace

use "$working/first_last_years.dta", clear
save "$workingnew/first_last_years.dta", replace

use "$working/wholesale_priceindex.dta", clear
save "$workingnew/wholesale_priceindex.dta", replace

global BS_fold = "$BSfoldnew" //  **UPDATING BS_fold TO BE A LOCAL FOLDER FOR EACH BOOTSTRAP;
global working = "$workingnew" //  **UPDATING working TO BE A LOCAL FOLDER FOR EACH BOOTSTRAP;

**Point Estimates
**CHOSE INSTRUMENTS + SPEC
global Zbase = "lag_l lag_m lag_e lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_lm lag_I_sq_le lag_I_sq_me lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km lag_I_sq_micro_inv_ke lag_I_sq_micro_inv_D_ke  lag2_l lag2_m lag2_e lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag2_I_sq_lm lag2_I_sq_le lag2_I_sq_me lag2_I_sq_micro_inv_km lag2_I_sq_micro_inv_D_km lag2_I_sq_micro_inv_ke lag2_I_sq_micro_inv_D_ke"
global ZnonX_base = "lag_D_ln_micro_inv lag_D_micro_inv lag_I_sq_micro_inv lag_I_sq_micro_inv_D lag_I_sq_micro_inv_km lag_I_sq_micro_inv_D_km lag_I_sq_micro_inv_ke lag_I_sq_micro_inv_D_ke lag2_D_ln_micro_inv lag2_D_micro_inv lag2_I_sq_micro_inv lag2_I_sq_micro_inv_D lag2_I_sq_micro_inv_km lag2_I_sq_micro_inv_D_km lag2_I_sq_micro_inv_ke lag2_I_sq_micro_inv_D_ke"
global INS =  "dm_lag_q_m dm_lag2_q_m dm_lag_l dm_lag_m dm_lag_e dm_lag_D_ln_micro_inv dm_lag_D_micro_inv dm_lag_I_sq_micro_inv dm_lag_I_sq_micro_inv_D dm_lag_I_sq_lm dm_lag_I_sq_le dm_lag_I_sq_me dm_lag_I_sq_micro_inv_km dm_lag_I_sq_micro_inv_D_km dm_lag_I_sq_micro_inv_ke dm_lag_I_sq_micro_inv_D_ke dm_lag2_l dm_lag2_m dm_lag2_e dm_lag2_D_ln_micro_inv dm_lag2_D_micro_inv dm_lag2_I_sq_micro_inv dm_lag2_I_sq_micro_inv_D dm_lag2_I_sq_lm dm_lag2_I_sq_le dm_lag2_I_sq_me dm_lag2_I_sq_micro_inv_km dm_lag2_I_sq_micro_inv_D_km dm_lag2_I_sq_micro_inv_ke dm_lag2_I_sq_micro_inv_D_ke"
local spec = "new_klme"


********************POINT ESTIMATES********************************************
do "$code/estimation/flexible/prod_est_Qklme_2lags.do"

global saveoutputs = "$working"
do "$code/estimation/flexible/D_est_TFPQIV_Qklme_2lags.do"

do "$code/estimation/flexible/data_work_Q_klme_TFPQIV_2lags.do" 
global datafiles = "decomposition_SS_ProdQ_new_klme_TFPQIV_2lags" 
do "$code/estimation/flexible/crosscheck.do"

global datafiles = "decomposition_SS_ProdQ_new_klme_TFPQIV_2lags" 
global samplename = "ProdQ_2lags"
do "$code/estimation/flexible/reg_shell_short.do" 

********************POINT ESTIMATES********************************************


***GRAB BS SAMPLES
do "$code/estimation/grabBS/06_Q_klme_2lags_grabBS.do"

*/
***RUN BS
local bsamplesize = 100

forvalues bs = 1(1)`bsamplesize' {
	*******Decomposition Sample*******
	global bs = `bs'
	
	clear 
	use "$BS_fold_old/record/Qklme/2lags/bssample_`bs'"
	
	save "$tempfold/bssample1", replace
	*Sample for second step (firm year)
	bysort co_code1 bs_co_code1 year: generate count_thing = _n
	keep if count_thing==1
	keep co_code1 bs_co_code1 year
	save "$tempfold/bssample2", replace

	do "$code/estimation/flexible/prod_est_Qklme_2lags_bs.do" 
	
	global saveoutputs = "$tempfold"
	do "$code/estimation/flexible/D_est_TFPQIV_Qklme_2lags_bs.do"
	
	global saveoutputs = "$tempfold"
	do "$code/estimation/flexible/data_work_Q_klme_TFPQIV_2lags.do" 
	global datafiles = "decomposition_SS_ProdQ_new_klme_TFPQIV_2lags" 
	do "$code/estimation/flexible/reg_shell_short_bs.do" 
	
}

global BS_fold = "$BS_fold_old" //  **UPDATING BS_fold TO BE THE MAIN PATH AGAIN;
global working = "$working_old" //  **UPDATING working TO BE THE MAIN PATH AGAIN;
global tempfold = "$tempfold_old"
global clean = "$clean_old"
